package cn.imvc.app.jobs;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.DocumentException;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.models.BiLogisticsMateModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.StorageOrderModel;
import cn.imvc.app.repositories.GoodsLimitTimeRepository;
import cn.imvc.app.repositories.GoodsRepository;
import cn.imvc.app.repositories.StorageOrderRepository;
@DisallowConcurrentExecution
public class BiServiceQuarz{
	@Autowired
	StorageOrderModel storageOrderModel;
	@Autowired
	StorageOrderRepository storageOrderRepository;
	@Autowired
	BiLogisticsMateModel biLogisticsMateModel;
	
	/**
	 * 自动去海关去订单号(30分钟)
	 * @param context
	 */
	public void autoEnableOrDisableGoods(JobExecutionContext context) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, -2);
		Date preDate = calendar.getTime();
		String nowStr = dateFormat.format(new Date());
		String preDateStr = dateFormat.format(preDate);
		//查询已发货，但没有快递号的订单
		List<Object[]> objsList = storageOrderRepository.findAllSql("select so.id, so.code from storage_order so where so.deliveryTime >= '"+preDateStr
				+"' and gl.endTime =< '"+nowStr+"' and so.expressCode is null");
		List<Object[]> objects = new ArrayList<>();
		try {
			objects = biLogisticsMateModel.logisticsMate(objsList);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator<Object[]> it = objects.iterator();
		while (it.hasNext()) {
			Object[] objects2 = (Object[]) it.next();
			storageOrderModel.updateBiLogisticsNo(objects2[0].toString(), objects2[1].toString());
		}
	}
}
